/* Author: David McCoy dave.mccoy@cospandesign.com
 *
 *     This file is part of DLCM\SubServ.
 *
 *  DLCM\SubServ is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DLCM\SubServ is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DLCM\SubServ.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * TextWidget.java
 *
 * Publish, and Subscribe to String/Text data over LCM
 *
 * Created on Mar 31, 2010, 12:35:14 AM
 */

package com.cospandesign.gui.pubsub.widgets;

import pubsub.PubSubManager;
import dlcm.Member;
import dlcm.Structure;

public class TextWidget extends javax.swing.JFrame implements Widget {

    String ChannelName;
    String MemberName;
    dlcm.Structure mStructure;
    dlcm.Member StringMember;
    //String OutString;
    PubSubManager Parent;

    /** Creates new form TextWidget */
    public TextWidget() {
        initComponents();
    }

// <editor-fold defaultstate="collapsed" desc="Static Functions">

    public static String GetWidgetName() {
        return "Text Box Widget";
    }
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Inherited Widget Functions">
    public boolean CanUseMember(Member member) {
        if (member.Data.getClass().equals(String.class)){
            return true;
        }
        return false;
    }
    public void ReceiveMessage(Structure structure){
        String inString = (String)structure.getMember(MemberName).Data;
        if (inString == null){
            return;
        }
        TextArea.append(inString);
        if (AppendCRCheckBox.isSelected()){
            TextArea.append("\n");
        }
    }
    public void SetupWidget(String channelName, String memberName, Structure structure, PubSubManager parent) {
        ChannelName = channelName;
        MemberName = memberName;
        mStructure = structure;
        /*mStructure = (Structure) structure.clone();
        try {
            mStructure.initialize();
        } catch (IOException ex) {
            Logger.getLogger(TextWidget.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(TextWidget.class.getName()).log(Level.SEVERE, null, ex);
        }
        */
        StringMember = mStructure.getMember(memberName);
        //OutString = (String)StringMember.Data;
        Parent = parent;
        this.setTitle(memberName + " Text Box");
        this.setVisible(true);

    }
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Functions">
    private void publishText (String SendString){
        //OutString = SendString;
        this.mStructure.getMember(MemberName).Data = SendString;
        Parent.PublishStructure(ChannelName, mStructure);
    }
    public void setWidgetVisibility(boolean visible){
        setVisible(visible);
    }
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Auto Generated Members">
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        SendTextField = new javax.swing.JTextField();
        AppendCRCheckBox = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        TextArea = new javax.swing.JTextArea();
        SendButton = new javax.swing.JButton();

        setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(getClass().getResource("/com/cospandesign/resources/cospandesign.png")));

        SendTextField.setName("SendTextField"); // NOI18N
        SendTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                SendTextFieldKeyPressed(evt);
            }
        });

        AppendCRCheckBox.setText("Append \\n");
        AppendCRCheckBox.setToolTipText("Automatically append a carriage return when a new string comes in");
        AppendCRCheckBox.setName("AppendCRCheckBox"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        TextArea.setColumns(20);
        TextArea.setRows(5);
        TextArea.setName("TextArea"); // NOI18N
        jScrollPane1.setViewportView(TextArea);

        SendButton.setText("Send");
        SendButton.setToolTipText("Accellerator = Ctrl + Enter");
        SendButton.setName("SendButton"); // NOI18N
        SendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SendButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
                    .addComponent(SendTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(AppendCRCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 217, Short.MAX_VALUE)
                        .addComponent(SendButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SendTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SendButton)
                    .addComponent(AppendCRCheckBox)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SendTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_SendTextFieldKeyPressed
        if (evt.getKeyChar() == evt.VK_ENTER) {
            publishText(SendTextField.getText());
        }
}//GEN-LAST:event_SendTextFieldKeyPressed

    private void SendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SendButtonActionPerformed
        publishText(SendTextField.getText());
}//GEN-LAST:event_SendButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox AppendCRCheckBox;
    private javax.swing.JButton SendButton;
    private javax.swing.JTextField SendTextField;
    private javax.swing.JTextArea TextArea;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables


// </editor-fold>
}
